#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "fps_counter.h"
#include "hero.h"
#include "key.h"
#include "write.h"
#include "definitions.h"
#include "toolkits/includes/glut.h"

extern GLvoid *font_style;
extern Fps_counter *fps_counter;
extern float fps;
extern float distance;
extern Key *keyClass;
extern int keyIDOnRadar;
extern Hero *heroClass;

Write::Write() {
}

void Write::setOrthographicProjection(){
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	gluOrtho2D(0, 200, 0, 200);
	glScalef(1, -1, 1);
	glTranslatef(0, -200, 0);
	glMatrixMode(GL_MODELVIEW);
}

void Write::resetPerspectiveProjection() {
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
}

void Write::setfont(char* name, int size) {
	font_style = GLUT_BITMAP_HELVETICA_18;
	if (strcmp(name, "helvetica") == 0) {
		if (size == 12) 
			font_style = GLUT_BITMAP_HELVETICA_12;
		else if (size == 18)
			font_style = GLUT_BITMAP_HELVETICA_18;
	} else if (strcmp(name, "times roman") == 0) {
		font_style = GLUT_BITMAP_TIMES_ROMAN_10;
		if (size == 24)
			font_style = GLUT_BITMAP_TIMES_ROMAN_24;
	} else if (strcmp(name, "8x13") == 0) {
		font_style = GLUT_BITMAP_8_BY_13;
	} else if (strcmp(name, "9x15") == 0) {
		font_style = GLUT_BITMAP_9_BY_15;
	}
}

void Write::writeText(GLuint x, GLuint y, char* format, ...) {
	va_list args;
	char buffer[255], *s;

	va_start(args, format);
	vsprintf_s(buffer, format, args);
	va_end(args);

	glPushMatrix();
	glLoadIdentity();

	setOrthographicProjection();

	glRasterPos2i(x, y);
	for (s = buffer; *s; s++)
		glutBitmapCharacter(font_style, *s);

	resetPerspectiveProjection();
	glPopMatrix();

}

void Write::writeInfo() {
	GLuint initY = 0;
	// generate strings
	char *fpsStr = new char[10];
	char *radarStr = new char[50];
	char *capKeysStr = new char[30];
	char *lifeStr = new char[10];

	fps_counter->fps_calc();
	sprintf(fpsStr, "FPS: %.2f", fps);
	if (distance != -1)
		sprintf(radarStr, "Key %d at %.2fm", keyIDOnRadar, distance);
	else
		sprintf(radarStr, "There aren't any keys nearby");
	sprintf(capKeysStr, "Captured %d of %d keys", keyClass->calcCapturedKeys(), KEYS); // number of captured keys
	sprintf(lifeStr, "Life: %d", heroClass->getLife());

	// font configs
	glDisable( GL_LIGHTING );
	glColor3ub(255, 255, 255);
	setfont("helvetica", 18);
	// write things
	writeText(1, initY+=7, fpsStr);
	writeText(1, initY+=7, radarStr);
	writeText(1, initY+=7, capKeysStr);
	writeText(1, initY+=7, lifeStr);
	glEnable( GL_LIGHTING );
}

void Write::writeGameOver(){
	GLuint sizex = glutGet(GLUT_WINDOW_WIDTH)/10;
	GLuint sizey = glutGet(GLUT_WINDOW_HEIGHT)/9;

	char *gmOvStr = new char[13];
	char *whatStr = new char[24];
	char *tryStr = new char[18];
	char *quitStr = new char[16];

	sprintf(gmOvStr, "GAME OVER...");
	sprintf(whatStr, "What do you want to do?");
	sprintf(tryStr, "Retry - Press 'R'");
	sprintf(quitStr, "Quit - Press 'Q'");

	glDisable( GL_LIGHTING );
	glColor3ub(255, 255, 255);
	setfont("helvetica", 18);

	writeText(sizex, sizey+=7, gmOvStr);
	writeText(sizex - 10, sizey+=7, whatStr);
	writeText(sizex - 1, sizey+=7, tryStr);
	writeText(sizex, sizey+=7, quitStr);
	glEnable( GL_LIGHTING );
}

void Write::writeWin(){
	GLuint sizex = glutGet(GLUT_WINDOW_WIDTH)/10;
	GLuint sizey = glutGet(GLUT_WINDOW_HEIGHT)/9;

	char *wnOvStr = new char[13];
	char *quitStr = new char[16];

	sprintf(wnOvStr, "YOU WON!!!");
	sprintf(quitStr, "Quit - Press 'Q'");

	glDisable( GL_LIGHTING );
	glColor3ub(255, 255, 255);
	setfont("helvetica", 18);

	writeText(sizex+1, sizey+=7, wnOvStr);
	writeText(sizex, sizey+=7, quitStr);
	glEnable( GL_LIGHTING );
}